﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2010
*/

#region Using
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;
using Rio.Doc.Business.Repository.Translator;
using NetBay.Core.Log;
using System;
using System.Data;
using Rio.Doc.DataAccess;
#endregion

namespace Rio.Doc.Business.Repository
{
    /// <summary>
    /// User Repository
    /// </summary>
    public class IndexRepository : IndexDer
    {

        /// <summary>
        /// Return Dictionary this Index (Posit and Id)
        /// </summary>
        /// <param name="s_FolderTypeTableName">Name of the s_ folder type table.</param>
        /// <returns></returns>
        public static Dictionary<long, long> GetIndexPosition(string s_FolderTypeTableName)
        {
            Dictionary<long, long> l_dIndexRetour = new Dictionary<long, long>();
            l_dIndexRetour = IndexTranslator.TranslateIndexPosition(DA_Index.GetIndex(s_FolderTypeTableName));
            return l_dIndexRetour;
        }

        /// <summary>
        /// Gets the folder index values.
        /// </summary>
        /// <param name="p_lFolderTypeId">The P_L folder type id.</param>
        /// <param name="p_lFolderID">The P_L folder ID.</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetFolderIndexValues(long p_lFolderTypeId, long p_lFolderID)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            using (IDataReader reader = DA_Index.GetFolderIndexValues(p_lFolderTypeId, p_lFolderID))
            {
                while (reader.Read())
                {
                    long id = reader.GetLongValue("RP_CH_NO");
                    int type = reader.GetIntValue("INDEX_TYPE");
                    string value = reader.GetStringValue("value");
                    result.Add("index" + id, GetFormattedValue(type, value));
                }
            }
            return result;
        }


        /// <summary>
        /// Gets the type of the index by folder.
        /// </summary>
        /// <param name="UserId">The user id.</param>
        /// <param name="FolderTypeIdentifier">The folder type identifier.</param>
        /// <returns></returns>
        public static List<Index> GetIndexByFolderType(long FolderTypeIdentifier)
        {
            return IndexTranslator.TranslateIndex(DA_Index.GetIndexByFolderType(FolderTypeIdentifier));
        }

        /// <summary>
        /// Gets the type of the index by folder.
        /// </summary>
        /// <param name="p_lUserId">The P_L user id.</param>
        /// <param name="p_lFolderTypeIdentifier">The P_L folder type identifier.</param>
        /// <returns></returns>
        public static List<Index> GetIndexByFolderType(long p_lUserId, long p_lFolderTypeIdentifier)
        {
            return IndexTranslator.TranslateIndex(DA_Index.GetIndexByFolderType(p_lUserId, p_lFolderTypeIdentifier));
        }

        /// <summary>
        /// Retrieve from a FolderType all folders that matches those criteria based on index
        /// </summary>
        /// <param name="p_lFolderTypeId">Folder type identifier</param>
        /// <param name="p_oCriteriaFilter">The p_o criteria filter.</param>
        /// <param name="p_lFolderId">The P_L folder id.</param>
        /// <returns>
        /// The list of folders filtered
        /// </returns>
        public static List<long> CheckUnicityOfIndexValue(long p_lFolderTypeId, Dictionary<long, string> p_oCriteriaFilter, long p_lFolderId)
        {
            List<long> l_oRet = new List<long>();
            try
            {
                string l_sIndexValue = string.Empty;
                IDataReader l_oIDataReader = null;
                //Loop on all index of the folder
                foreach (KeyValuePair<long, string> l_oKvp in p_oCriteriaFilter)
                {
                    //if the user did not put a value for this index : nothing to do
                    if (l_oKvp.Value != string.Empty)
                    {
                        l_sIndexValue = Epure(l_oKvp.Value);
                        l_sIndexValue = DoubleQuote(l_sIndexValue);
                        l_sIndexValue = EncapsulateQuote(l_sIndexValue);

                        l_oIDataReader = DA_Index.CheckUnicityOfIndexValue(p_lFolderTypeId, l_oKvp.Key, l_sIndexValue, p_lFolderId);

                        using (l_oIDataReader)
                        {
                            // Read all rows
                            while (l_oIDataReader.Read())
                            {
                                l_oRet.Add(l_oKvp.Key);
                                break;
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(ex.Message);
                l_oRet = new List<long>();
            }
            return l_oRet;
        }


        #region Private method


        private static string Epure(string p_szSource)
        {
            return p_szSource.TrimStart(new[] { '\'' }).TrimEnd(new[] { '\'' });
        }

        private static string DoubleQuote(string p_szSource)
        {
            return p_szSource.Replace("'", "''");
        }

        private static string EncapsulateQuote(string p_szSource)
        {
            return "'" + p_szSource + "'";
        }

        private static string GetFormattedValue(int indexType, string rawValue)
        {
            string formattedValue = rawValue;

            if (!String.IsNullOrWhiteSpace(formattedValue))
                switch (indexType)
                {
                    case 4: // IndexType.Date:
                        formattedValue = String.Format("{0:dd/MM/yyyy}", DateTime.Parse(formattedValue));
                        break;
                }

            return formattedValue;
        }

        #endregion
    }
}
